﻿@using Orchard.Utility.Extensions;
@{
    Style.Require("Contrib.Stars");
    Script.Require("Contrib.Stars");

    double roundedResult = Math.Round(Model.ResultValue*2, MidpointRounding.AwayFromZero)/2;
    string currentResultClass = string.Format("stars-value-{0}", Math.Floor(roundedResult));

    double userRating = Model.UserRating;
    bool userCanRate = Request.IsAuthenticated || Model.AllowAnonymousRatings;
    if (userCanRate && !Request.IsAuthenticated && Request.Cookies["Orchrd__stars"] != null && !string.IsNullOrWhiteSpace(Request.Cookies["Orchrd__stars"].Value)) {
        userRating = Convert.ToDouble(Request.Cookies["Orchrd__stars"].Value); // <- info: (heskew) way too easy for this to get out of sync accidently
    }
    string canRateClass = userCanRate ? "stars-can-rate" : "";
    string userRatingClass = userCanRate && userRating > 0 ? string.Format("stars-user-rating-{0}", userRating) : "";
}
<div class="stars-rating @canRateClass">
    <div class="stars-current-result @currentResultClass @userRatingClass">
        @if (roundedResult == .5){
        <span class="star-0-5 a-half-star">@T(".").ToString()</span>
        <span class="star-1 a-star a-half-star">@T("-").ToString()</span>
        }
        else{
        <span class="star-1 a-star">@(Model.ResultValue > 0 ? T("*").ToString() : T("-").ToString())</span>
        }
        @if (roundedResult == 1.5){
        <span class="star-1-5 a-half-star">@T(".").ToString()</span>
        <span class="star-2 a-star a-half-star">@T("-").ToString()</span>
        }
        else{
        <span class="star-2 a-star">@(Model.ResultValue > 1 ? T("*").ToString() : T("-").ToString())</span>
        }
        @if (roundedResult == 2.5){
        <span class="star-2-5 a-half-star">@T(".").ToString()</span>
        <span class="star-3 a-star a-half-star">@T("-").ToString()</span>
        }
        else{
        <span class="star-3 a-star">@(Model.ResultValue > 2 ? T("*").ToString() : T("-").ToString())</span>
        }
        @if (roundedResult == 3.5){
        <span class="star-3-5 a-half-star">@T(".").ToString()</span>
        <span class="star-4 a-star a-half-star">@T("-").ToString()</span>
        }
        else{
        <span class="star-4 a-star">@(Model.ResultValue > 3 ? T("*").ToString() : T("-").ToString())</span>
        }
        @if (roundedResult == 4.5){
        <span class="star-4-5 a-half-star">@T(".").ToString()</span>
        <span class="star-5 a-star a-half-star">@T("-").ToString()</span>
        }
        else{
        <span class="star-5 a-star">@(Model.ResultValue > 4 ? T("*").ToString() : T("-").ToString())</span>
        }
    </div>
    @if (userCanRate) {
        using (Html.BeginFormAntiForgeryPost(Url.Action("Apply", "Rate", new { area = "Contrib.Stars" }), FormMethod.Post, new {@class = "rate"})) {
            <label for="starsRateActions-@Model.ContentItem.Id">@T("Rate:")</label>
            <select id="starsRateActions-@Model.ContentItem.Id" name="rating">
                @Html.SelectOption(userRating, 0, T("give your opinion").ToString())
                @Html.SelectOption(userRating, 1, T("*").ToString())
                @Html.SelectOption(userRating, 2, T("**").ToString())
                @Html.SelectOption(userRating, 3, T("***").ToString())
                @Html.SelectOption(userRating, 4, T("****").ToString())
                @Html.SelectOption(userRating, 5, T("*****").ToString())
                @Html.SelectOption(userRating, -1, T("clear your opinion").ToString())
            </select>
            @Html.Hidden("contentId", (int)Model.ContentItem.Id)
            @Html.Hidden("ReturnUrl", Context.Request.ToUrlString()) 
            <button type="submit" name="submit.BulkEdit" value="yes">@T("Save")</button>
        }
    }
</div>